Date.prototype.format = function (format = "y-m-d hh:MM") {
  return uni.$u.timeFormat(this.getTime(), format);
};

Date.prototype.getNextMonth = function (gap = 1) {
  const date = new Date(this.getFullYear(), this.getMonth() + gap);
  return date;
};
Date.prototype.getMonthEnd = function () {
  return new Date(this.getNextMonth() - 1e3);
};

Date.prototype.getNextDay = function (gap = 1) {
  const date = new Date(
    this.getFullYear(),
    this.getMonth(),
    this.getDate() + gap
  );
  return date;
};
Date.prototype.getToday = function () {
  return new Date(this.getFullYear(), this.getMonth(), this.getDate());
};
Date.prototype.getDayEnd = function () {
  return new Date(this.getToday() * 1 + 86400e3 - 1e3);
};
Date.prototype.toISO8String = function () {
  return this.format("y-m-d hh:MM:ss");
};

String.prototype.toDate = function () {
  //
};

Date.prototype.toNiceTime = function (now, mode = 1) {
  const date = this;
  // console.log(now, date)
  const second = ((now * 1 - date) / 1e3) | 0;
  if ((mode == 1 || mode == 11) && second > 0) {
    if (second < 10) return "刚刚";
    if (second < 60) return second - (second % 10) + "秒前";
    if (second < 60 * 60) return Math.floor(second / 60) + "分钟前";
    if (mode == 11) {
      if (second < 86400) return Math.floor(second / 3600) + "小时前";
      return Math.floor(second / 86400) + "天前";
    }
  }
  let datePart = date.format("yy-mm-dd");
  const timePart = date.format("hh:MM");
  const today = now.getToday();
  if (date.getFullYear() == now.getFullYear()) datePart = date.format("m月d日");
  if (mode == 2) {
    return datePart;
  }
  if (second < 0) {
    const nextDay = now.getNextDay();
    if (date >= nextDay && date <= nextDay.getDayEnd()) datePart = "明天";
  } else if (date >= today) datePart = "今天";
  else if (date >= today.getNextDay(-1)) datePart = "昨天";
  return datePart + " " + timePart;
};

String.prototype.cutStr = function (pre = 100, trail = 0, rep = "...") {
  if (this.length <= pre + trail + rep.length) return this.toString();
  let txt = this.substring(0, pre - 3) + rep;
  if (trail) txt += this.substr(-trail);
  return txt;
};

String.prototype.getLen = function () {
  return this.toString().replace(/[^\x00-\xff]/g, "++").length;
};

if (!String.prototype.replaceAll) {
  String.prototype.replaceAll = function (word, tar) {
    const reg = new RegExp(word, "mg");
    return this.replace(reg, tar);
  };
}

Array.sortArrayBy = function (arr, key, isDesc) {
  arr.sort((a, b) => {
    return (isDesc ? -1 : 1) * (a[key] - b[key]);
  });
  return arr;
};
// Array.prototype.sortBy 遍历时需增加判断 arr.hasOwnProperty(key)
